<?php
	/**
	* @package ParkFind
	* @desc This is the park details php script. This script is used to display the park details.
	*/
	/**
	* @desc This include file will load the directory settings for the Smarty Templates
	*/
	
	// Direct calling check
	if (!isset($_GET["id"]))
		die("Do not call this page directly");
	
	session_start();

	//Smarty
	require 'includes/smarty.inc';
	$smarty->assign('user_id', $_SESSION["user_id"]);
	$smarty->assign('last', $_SESSION["lastviewed"]);	
	$smarty->assign('firstname', $_SESSION["firstname"]);
	$smarty->assign('top5', $_SESSION["top5"]);	
	$smarty->assign('role', $_SESSION["role"]);
	
	/**
	* @desc This include file will load the functions that are required to call 
	*		from the database
	*/
	require 'includes/db_functions.inc';
	/**
	* @desc This include file connects to the database.
	*/
	require 'includes/connect.inc';


	// Get variables
	$park_id = mysql_real_escape_string($_GET["id"], $connection);	
	$commrec = $_GET["commrec"];
	$commpage = $_GET["page"];
	$ratingrec = $_GET["ratingrec"];
	$delrec = $_GET["delrec"];
	
	
	// Get the park info
	$parkinfo = getParkInfo($park_id);
	
	// Check if park exists
	if ($parkinfo == FALSE)
	{
		echo "No park found";
		exit;			
	}
	
	// Get park details	
	$row = mysql_fetch_array($parkinfo);	
	
	// assign last bits of info to header
	$smarty->assign('park_id', $park_id);
	$smarty->assign('address', $row["address"]);
	$smarty->assign('postcode', $row["postcode"]);
	$smarty->assign('suburb', $row["subname"]);
	$smarty->assign('name', $row["name"]);
	$smarty->assign('title', $row["name"] . " - Details");
	
	$smarty->display('header.tpl');
	
	
	// Pass general info if any
	$smarty->assign('user_id', $_SESSION["user_id"]);
	$smarty->assign('curURL', curPageURL());
	$smarty->assign('park_id', $park_id);	
	
	// Pass park details	
	
	//check if pic exists
	$query = "SELECT id FROM pictures WHERE id = $park_id";
	
	$picexists = mysql_query($query, $connection);
	if(mysql_num_rows($picexists) > 0)
		$src = "showpic.php?id=$park_id";
	else
		$src = "images/noimage.png";
	
	
	$smarty->assign('img', $src);	
	$smarty->assign('type', $row["type"]);
	$smarty->assign('name', $row["name"]);
	$smarty->assign('address', $row["address"]);	
	$smarty->assign('suburb', $row["subname"]);
	$smarty->assign('postcode', $row["postcode"]);	
	
	// Icon types go here
	if ($row["type"] == "Skate Parks")
		$smarty->assign('typeicon', "images/skate.png");
	
	// Add it to last viewed
	addLastViewed($row["park_id"], $row["name"]);
	
	
	// Equipment					
	$equipment = getEquipment($park_id);
	
	$arEquip = array();
	
	if ($equipment == TRUE)
	{	
		// Iterate equipment
		while ($row = mysql_fetch_array($equipment))										
			$arEquip[] = array('qty' => $row["qty"], 'name' => $row["name"]);	
	}	
	
	$smarty->assign('equip', $arEquip); // Assign array even if empty	

	
	//Comment confirmation message
	if ($commrec == "success")
		$smarty->assign('commsg', "Thank you for you comment.</br>");
		
	else if($commrec == "failed")
		$smarty->assign('commsg', "Sorry, due to an error your comment was not posted.</br>");
	
	//Comment delete message
	if ($delrec == "success")
		$smarty->assign('delmsg', "Your comment has been deleted.</br>");
		
	else if($delrec == "failed")
		$smarty->assign('delmsg', "Sorry, due to an error your comment was not deleted.</br>");

	// Rating confirmation message
	if ($ratingrec == "success")
		$smarty->assign('ratingmsg', "Thank you for rating.</br>");
	else if($ratingrec == "failed")
		$smarty->assign('ratingmsg', "Sorry, your rating was not submitted.</br>");
		
	//Pass rating to smarty
	$rating = hasRated($_SESSION["user_id"], $park_id);
	$smarty->assign('rating', $rating);

	
	//Comments	
	
	if (!$commpage)
		$commpage = 1;
	
	$comments = getCommentsPage($commpage);

	$arComments = array();
	
	if ($comments == TRUE)
	{
		// Iterate comments
		while ($row = mysql_fetch_array($comments))										
			$arComments[] = array('name' => $row["firstname"],
									'user_id' => $row["user_id"],
									'comment' => $row["comment"],
									'id' => $row["comment_id"]);						
	}

	$smarty->assign('comments', $arComments); // Assign array even if empty
	$smarty->assign('prevpage', $commpage - 1); // Assign comment limit so it only shows a few at a time
	$smarty->assign('nextpage', $commpage + 1); // Assign comment limit so it only shows a few at a time
	
	$smarty->display('parkdetails.tpl');
	
	mysql_close($connection); // Close connection
	
	$smarty->display('footer.tpl');
	
	// ***** End of procedural script ******
	
	/**
	 *@desc This function gets the comment information from the database.
	 *@param int $page
	 */
	function getCommentsPage($page)
	{
		global $park_id; // Make accessible in here
		
		$page--; // 1 Bound
		
		$comments = getComments($park_id, ($page * 5), 5);
		return $comments;
	}
	
	/**
	 *@desc This function gets the last view park information from the session vaiables. These parks are displayed on the right side of our site.
	 *@param int $park_id
	 *@param string $name
	 */
	function addLastViewed($park_id, $name)
	{
	
		// Last viewed
		if (!isset($_SESSION["lastviewed"]))
			$_SESSION["lastviewed"] = array();
			
		//Check for dupes
		foreach ($_SESSION["lastviewed"] as $elem)	
			if ($elem["id"] == $park_id)
				$dupe = TRUE;
		
		if ($dupe == TRUE)
			return; // Leave prematurely

		// If equals 5, delete last element then...
		if (count($_SESSION["lastviewed"]) == 5)
			unset ($_SESSION ["lastviewed"] [4]);

		// ...push onto array
		array_unshift($_SESSION["lastviewed"], array('id' => $park_id,
			'name' => $name));
	}
	
	/**
	 *@desc This function is used to grab the current URL information. Implemented for FaceBook and Twitter like button.
	 */
	function curPageURL() 
	{
	   $pageURL = 'http';
	   if ($_SERVER["HTTPS"] == "on") 
	   {
		  $pageURL .= "s";
	   }
	   
	   $pageURL .= "://";
	   if ($_SERVER["SERVER_PORT"] != "80") 
	   {
		  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	   } 
	   else 
	   {
		  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	   }
	   return $pageURL;
	}
	
	
?>
